package com.example.dairenkun.neufoodshop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.adapter.FoodListAdapter;
import com.example.dairenkun.neufoodshop.bean.Buy;
import com.example.dairenkun.neufoodshop.bean.Food;
import com.example.dairenkun.neufoodshop.bean.Order;
import com.example.dairenkun.neufoodshop.bean.doCollect;
import com.example.dairenkun.neufoodshop.bean.isCollected;
import com.example.dairenkun.neufoodshop.model.FoodModel;
import com.example.dairenkun.neufoodshop.model.OrderModel;
import com.example.dairenkun.neufoodshop.service.FoodService;
import com.example.dairenkun.neufoodshop.service.OrderService;
import com.squareup.picasso.Picasso;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodDetailActivity extends AppCompatActivity {
    private TextView FoodDetailName;
    private TextView FoodDetailPrice;
    private ImageView FoodDetailPic;
    private TextView FoodDetailIntro;
    private TextView FoodDetailIntroContent;
    private Button FoodDetialBtn;
    private ImageButton btn_food_collect;
    private  TextView FoodRecommand;
    private  Button btn_food_browse;

    int foodid;
    int userid;
    String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        initViews();
        getData();
        isCollect();

    }
    public void initViews(){
       FoodDetailPic=(ImageView)findViewById(R.id.foodDetail_pic);
        FoodDetailName=(TextView)findViewById(R.id.foodDetail_name);
        FoodDetailPrice=(TextView)findViewById(R.id.foodDetail_price);
        FoodDetailIntro=(TextView)findViewById(R.id.foodDetail_intro);
        FoodDetailIntroContent=(TextView)findViewById(R.id.foodDetail_introContent);
        FoodDetialBtn=(Button)findViewById(R.id.btn_foodDetail_buy);
        btn_food_collect=(ImageButton)findViewById(R.id.foodDetail_imageBtn_collect);
        FoodRecommand=(TextView)findViewById(R.id.foodDetail_recommand);
        btn_food_browse=(Button) findViewById(R.id.btn_foodDetail_browsecomment);
    }
    private void getData(){
        Intent intent = getIntent();
        foodid = Integer.valueOf(intent.getStringExtra("food_id"));
      /*  //1
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://60.205.189.39/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //2
        FoodService service = retrofit.create(FoodService.class);
        Call<Food> call = service.getFoodDetail(foodid);*/
        FoodModel foodModel=new FoodModel();
        Call call=foodModel.FoodDetail(foodid);
        //3
        call.enqueue(new retrofit2.Callback<Food>() {
            @Override
            public void onResponse(retrofit2.Call<Food> call, retrofit2.Response<Food> response) {
              final  Food food = response.body();
                FoodDetailName.setText(food.getFoodname());
                FoodDetailIntroContent.setText(food.getIntro());
                //点击将TextView中的内容全部显示。收缩
                FoodDetailIntroContent.setOnClickListener(new View.OnClickListener() {
                    Boolean flag = true;
                    @Override
                    public void onClick(View v) {
                        Log.i("tv.getLineCount()",FoodDetailIntroContent.getHeight()+"");
                        if(flag){

                            flag = false;
                            FoodDetailIntroContent.setEllipsize(null); // 展开
                            FoodDetailIntroContent.setSingleLine(flag);
                        }else{
                            flag = true;
                            FoodDetailIntroContent.setEllipsize(TextUtils.TruncateAt.END); // 收缩
                            FoodDetailIntroContent.setSingleLine(flag);
                        }
                    }
                });
                FoodDetailPrice.setText(food.getPrice() + "元");
                if (!"0".equals(food.getRecommand()))
                {
                    FoodRecommand.setText("热销！");
                }
                else
                {
                    FoodRecommand.setText("");
                }
                if(food.getPic().equals(""))
                    Picasso.with(FoodDetailActivity.this).load(R.mipmap.ic_launcher).into(FoodDetailPic);
               else Picasso.with(FoodDetailActivity.this).load(food.getPic()).into(FoodDetailPic);

                //查看评论
                btn_food_browse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getApplication(),FoodCommentActivity.class);
                        intent.putExtra("food_id",food.getFood_id()+"");
                        startActivity(intent);
                    }
                });
                //购买
                FoodDetialBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getApplication(),BuyActivity.class);
                        intent.putExtra("food_id",food.getFood_id()+"");
                        intent.putExtra("price",food.getPrice()+"");
                        intent.putExtra("foodname",food.getFoodname());
                        intent.putExtra("pic",food.getPic());
                        startActivity(intent);
                    }
                });


            }

            @Override
            public void onFailure(retrofit2.Call<Food> call, Throwable t) {

            }
        });
        //收藏
        btn_food_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCollect();
            }
        });
    }

    private  void  doCollect(){
        FoodModel foodModel=new FoodModel();
        Call call=foodModel.doCollectfood(userid,foodid);
        call.enqueue(new Callback<doCollect>() {
            @Override
            public void onResponse(Call<doCollect> call, retrofit2.Response<doCollect> response)
            {
                doCollect tmp=response.body();
                String   status =tmp.getSuccess();
                if (status.equals("1"))
                {
                    if (result.equals("0"))
                    {
                        btn_food_collect.setImageResource(R.drawable.ic_favorite_black_24dp);
                        Toast.makeText(FoodDetailActivity.this, "收藏成功！", Toast.LENGTH_SHORT).show();
                        result="1";
                    }else
                    {
                        btn_food_collect.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                        Toast.makeText(FoodDetailActivity.this, "取消收藏成功！", Toast.LENGTH_SHORT).show();
                        result="0";
                    }

                }
                else {
                    Toast.makeText(FoodDetailActivity.this, "取消失败！", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<doCollect> call, Throwable t) {

            }
        });
    }
    private  void isCollect(){

        FoodModel foodModel=new FoodModel();
        Call call=foodModel.isCollect(userid,foodid,1);
        call.enqueue(new Callback<isCollected>() {
            @Override
            public void onResponse(Call<isCollected> call, retrofit2.Response<isCollected> response)
            {
                isCollected res=response.body();
                result =res.getCollected().toString();
                if (result.equals("0"))
                {
                    btn_food_collect.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                }
                else {
                    btn_food_collect.setImageResource(R.drawable.ic_favorite_black_24dp);
                }

            }
            @Override
            public void onFailure(Call<isCollected> call, Throwable t) {

            }
        });
    }
    private void getUser(){
        SharedPreferences sp = getApplication().getSharedPreferences("UserInfo", getApplication().MODE_PRIVATE);
        userid = Integer.valueOf(sp.getString("user_id", null));


    }

}